package com.fh.generic;

import java.util.ArrayList;

@SuppressWarnings({"all"})
public class Generic02 {
    public static void main(String[] args) {
        //使用传统的方法来解决===> 使用泛型
        //1. 当我们 ArrayList<Dog> 表示存放到 ArrayList 集合中的元素是 Dog 类型
        //2. 如果编译器发现添加的类型，不满足要求，就会报错
        //3. 在遍历的时候，可以直接取出 Dog 类型而不是 Object
        //4. public class ArrayList<E> {} E 称为泛型,那么 Dog->E
        ArrayList<Dog1> dog1s = new ArrayList<>();
        dog1s.add(new Dog1("旺财", 10));
        dog1s.add(new Dog1("发财", 1));
        dog1s.add(new Dog1("小黄", 5));
        System.out.println("======使用泛型======");
        //此时当我们不小心添加了一只猫，编译器会自动识别，无需运行后才知道语法有错误
//        dog1s.add(new Cat("小花",5));
        for (Dog1 dog1 : dog1s) {
            System.out.println(dog1.getName()+"-"+ dog1.getAge());
        }
    }
}
class Dog2{
    private String name;
    private int age;
    public Dog2(String name,int age){
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
class Cat2 { //Cat 类
    private String name;
    private int age;
    public Cat2(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
